//from: http://west-wind.com/weblog/posts/1109.aspx

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;


namespace stsdev
{
	/// <summary>
	/// Summary description for dsTextBox.
	/// </summary>
 	[ToolboxBitmap(typeof(TextBox)),
	DefaultProperty("Text")]
	public class wwTextBox :  System.Windows.Forms.TextBox
	{

		/// <summary>
		/// Internally stored reference to the parent container so we can bind back on a per field level
		/// </summary>
		protected Control ParentContainer = null;

		public wwTextBox() 
		{ 
		}

	
		/// <summary>
		/// Determines the type of input that we're dealing with. Numeric display automatically
		/// sets a few defaults and prevents certain keys from being input.
		/// </summary>
		public TextBoxInputTypes InputType
		{ 
			get 
			{ 	return this.eInputType;	}
			set
			{	
				this.eInputType = value;
				if (value == TextBoxInputTypes.Numeric)
					this.TextAlign = HorizontalAlignment.Right;
				else
					this.TextAlign = HorizontalAlignment.Left;
			}
		}
		TextBoxInputTypes eInputType = TextBoxInputTypes.Text;

				
		/// <summary>
		/// IsNumeric check routine
		/// </summary>
		/// <param name="e"></param>
		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			if (this.eInputType == TextBoxInputTypes.Text)
				return;

			char ch = e.KeyChar;

			if (this.InputType == TextBoxInputTypes.Numeric)
			{
				// *** if it's numeric char allow it
				if (ch >= '0' &&  ch <= '9'  || 
					ch == '.' || ch == ',' || ch == '-' || ch == 8)
					return;

				// *** otherwise don't accept character
				e.Handled = true;
			}
			else if (this.InputType == TextBoxInputTypes.Date) 
			{
				// *** if it's numeric char allow it
				if (ch >= '0' &&  ch <= '9'  || 
					ch == '.' || ch == ',' || ch == '-' || ch == '/' || ch == 8 || 
					ch == 'a' || ch == 'p' || ch == 'm' || ch == ' ')
					return;

				// *** otherwise don't accept character
				e.Handled = true;

			}
				
		}
	}

	public enum TextBoxInputTypes
	{
		Text,
		Numeric,
		Date
	}
}
